﻿@page "/Shop"
@using HuiLianMedical.Share
@using HuiLianMedical.Share.Data
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<MedicalContext> dbContextFactory

<Drawer Closable="true" Width="600" Visible="Visible"
        Title='"更改"' OnClose="@CloseTable"
        Mask="false">
    <Form Model="ChangeModel"
          RequiredMark="FormRequiredMark.Optional"
          ValidateMode="@FormValidateMode.Rules"
          OnFinish="OnFinish">
        <FormItem Class="item" Label="名称" >
            <Input @bind-Value="@context.Name"/>
        </FormItem>
        <FormItem Class="item" Label="描述">
            <Input @bind-Value="@context.Description">
        </FormItem>
        <FormItem Class="item" WrapperColOffset="8" WrapperColSpan="16">
            <Button Type="@ButtonType.Primary" HtmlType="submit">提交</Button>
        </FormItem>
    </Form>
</Drawer>

<Table TItem="CategoryModel" DataSource="@CategoryList" Responsive>
    <PropertyColumn Title="名称" Property="c => c.Name"/>
    <PropertyColumn Title="编号" Property="c => c.Key"/>
    <PropertyColumn Title="描述" Property="c => c.Description"/>
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Danger OnClick="@(() => Delete(context))">删除</Button>
            </SpaceItem>
            <SpaceItem>
                <div class="phone-left-display">
                    <Button Danger OnClick="@(() => OpenTable(context))">更改</Button>
                </div>
            </SpaceItem>
            <SpaceItem>
                <div class="phone-left-display">
                    <Button Danger OnClick="@(() => OpenTable(context))">更改</Button>
                </div>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@code {
    
    private bool Visible;
    
    private List<CategoryModel> CategoryList { get; set; } = [];
    
    protected override async Task OnInitializedAsync()
    {
        await using var context = await dbContextFactory.CreateDbContextAsync();
        CategoryList = await context.Categories
            .Include(x => x.Commodities)
            .ToListAsync();
    }

    private async Task Delete(CategoryModel model)
    {
        await using var context = await dbContextFactory.CreateDbContextAsync();
        context.Categories.Remove(model);
        await context.SaveChangesAsync();
        CategoryList.Remove(model);
    }

    private CategoryModel ChangeModel { get; set; } = new();

    private void CloseTable()
    {
        Visible = false;
        ChangeModel = new CategoryModel();
    }

    private void OpenTable(CategoryModel model)
    {
        Visible = true;
        ChangeModel = model;
    }

    private async Task OnFinish()
    {
        await using var context = await dbContextFactory.CreateDbContextAsync();
        var f = await context.Categories.FirstOrDefaultAsync(x => x.Key == ChangeModel.Key);

        if (f == null) return;

        f.Name = ChangeModel.Name;
        f.Description = ChangeModel.Description;

        await context.SaveChangesAsync();
    }

}